package qiyun


import (
	"github.com/Aiicy/htmlquery"
	"github.com/ruoklive/proxypool/internal/ipgetter/base"
	"github.com/ruoklive/proxypool/pkg/models"
	"github.com/ruoklive/proxypool/pkg/register"
	"go.uber.org/zap"
	"io"
)

func init() {
	register.Add(func() register.IPGetter {
		return New()
	})
}

type IPGetter struct {
	base.Base
}

func New() *IPGetter {
	g := &IPGetter{}
	g.Base = base.NewBase("http://proxy.ip3366.net/free/", g.Parse, "qiyun")
	return g
}

// KDL get ip from kuaidaili.com
func (g *IPGetter) Parse(reader io.ReadCloser) (result []*models.IP) {

	doc, _ := htmlquery.Parse(reader)
	trNode, err := htmlquery.Find(doc, "//table[@class='table table-bordered table-striped']//tbody//tr")
	if err != nil {
		g.Error("Parse fail!", zap.Error(err))
	}

	for i := 0; i < len(trNode); i++ {

		tdNode, _ := htmlquery.Find(trNode[i], "//td")
		kind := htmlquery.InnerText(tdNode[2])
		if kind != "高匿"{
			continue
		}

		IP := models.NewIP()
		IP.Data = htmlquery.InnerText(tdNode[0]) + ":" + htmlquery.InnerText(tdNode[1])

		if htmlquery.InnerText(tdNode[3]) == "HTTP" {
			IP.Type1 = "http"
		} else {
			IP.Type1 = ""
			IP.Type2 = "https"
		}

		g.Info("GET IP", zap.String("ip", IP.String()))
		result = append(result, IP)
	}
	g.Info("[qiyun] done.")
	return
}

